package com.datas.service.api;

import com.datas.beans.SearchCompanyParam;
import com.datas.beans.SearchCompanyRes;
import com.datas.db.hibernatebeans.UmsRegionCity;
import com.datas.db.hibernatebeans.UmsRegionCountry;
import com.datas.db.hibernatebeans.UmsRegionProvince;
import com.datas.db.hibernatebeans.UmsRegionTown;
import com.datas.utils.hbn.HbnTxRuner;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by jige0727 on 2017/5/31.
 * 地区的api接口
 */
@Service
public class ApiRegionService {
    public JSONObject region_all() {
        return (JSONObject) new HbnTxRuner() {
            @Override
            protected Object dataLogic() throws Exception {
                JSONObject regionData = new JSONObject();
                for (UmsRegionProvince province : provinceList(getCurrentSession())) {
                    regionData.put(
                            province.getCode(), new JSONObject()
                                    .put("name", province.getName())
                                    .put("sap_code", province.getSap_code())
                                    .put("cities", new JSONObject())
                    );
                }
                for (UmsRegionCity city : cityList(getCurrentSession())) {
                    String provinceCode = city.getRegionProvince_code();
                    regionData.getJSONObject(provinceCode).getJSONObject("cities")
                            .put(
                                    city.getCode(), new JSONObject()
                                            .put("name", city.getName())
                                            .put("towns", new JSONObject())
                            );

                }
                for (UmsRegionTown town : townList(getCurrentSession())) {
                    String provinceCode = town.getRegionProvince_code();
                    String cityCode = town.getRegionCity_code();
                    String townCode = town.getCode();
                    regionData.getJSONObject(provinceCode).getJSONObject("cities").getJSONObject(cityCode).getJSONObject("towns")
                            .put(
                                    town.getCode(), new JSONObject()
                                            .put("name", town.getName())
                            );
                }
                return regionData;
            }
        }.run();
    }

    private List<UmsRegionProvince> provinceList(Session session) {
        return session.createQuery("from UmsRegionProvince ").list();
    }

    private List<UmsRegionCity> cityList(Session session) {
        return session.createQuery("from UmsRegionCity ").list();
    }

    private List<UmsRegionTown> townList(Session session) {
        return session.createQuery("from UmsRegionTown ").list();

    }

    public JSONObject region_country() {
        return (JSONObject) new HbnTxRuner() {
            @Override
            protected Object dataLogic() throws Exception {
                JSONObject regionData = new JSONObject();
                List<UmsRegionCountry> list = getCurrentSession().createQuery("from UmsRegionCountry ").list();
                for (UmsRegionCountry province : list) {
                    regionData.put(
                            province.getCode(), new JSONObject()
                                    .put("name", province.getName())
                    );
                }
                return regionData;
            }
        }.run();
    }

    public JSONArray searchCompany(SearchCompanyParam param) {
        return (JSONArray) new HbnTxRuner() {
            @Override
            protected Object dataLogic() throws Exception {
                String hql = "select new com.datas.beans.SearchCompanyRes(i.id, i.dept_name, i.dept_code) " +
                        "from UmsDepartment i where 1=1 " +
                        "AND i.struct LIKE '%-%' " +
                        "AND i.struct NOT LIKE '%-%-%' " +
                        (StringUtils.isNotBlank(param.getDept_nameSearch()) ? "and i.dept_name like :dept_name " : "") +
                        (StringUtils.isNotBlank(param.getDept_codeSearch()) ? "and i.dept_code like :dept_code " : "");
                List<SearchCompanyRes> list =
                        getCurrentSession().createQuery(hql)
                                .setProperties(param.param()).list();
                return new JSONArray(list);
            }
        }.run();
    }
}
